﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography;


namespace HistoryToMail
{
    public class SMTPSettings
    {
        public String serverAddress = "";
        public int serverPort;
        public Boolean serverUseSSL = false;
        public Boolean serverUseAuth = false;
        public String serverUserName = "";
        public byte[] encrServerPassword = null;
        public String senderAddress = "";
        public String replyAddress = "";
        public String destinationAddress = "";

        [System.Xml.Serialization.XmlIgnoreAttribute]
        public String serverPassword
        {
            get
            {
                return DecryptData(encrServerPassword);
            }
            set 
            {
                encrServerPassword = EncryptData(value);
            }
        }

        private byte[] EncryptData(string data)
        {
            byte[] unprotectedData = System.Text.Encoding.Unicode.GetBytes(data);
            return ProtectedData.Protect(unprotectedData, null, DataProtectionScope.CurrentUser);
        }

        private string DecryptData(byte[] encryptedData)
        {
            if (encryptedData == null)
            {
                return "";
            }
            byte[] data = ProtectedData.Unprotect(encryptedData, null, DataProtectionScope.CurrentUser);
            String normalrepresentation = System.Text.Encoding.Unicode.GetString(data);
            return normalrepresentation;
        }


        public void showSettingsDialog()
        {
            SMTPSettingsForm dialog = new SMTPSettingsForm(this);
            dialog.ShowDialog();
        }

        /*override public String ToString()
        {
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(this.GetType());
            StringWriter Output = new StringWriter(new StringBuilder());
            x.Serialize(Output, this);
            return Output.ToString();
        }*/

        /*public static SMTPSettings FromString(String data)
        {
            System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(QIP.SMTPSettings));
            StringReader Input = new StringReader(data);
            return (SMTPSettings)x.Deserialize(Input);
        }*/
    }
}
